﻿namespace EFS.RC.CC.Applications.Remote.Operators
{
	/// <summary>
	/// Supervisor session
	/// </summary>
	internal class SuperUser : EFS.RC.CC.Applications.Remote.Application
	{
		#region Composition

		// sync access
		private object syncAccess = new object();

		// Operator
		private EFS.RC.CC.Presences.Operators.Anonymous annonimousPresence = null;

		#endregion

		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="annonimousPresence"></param>
		public SuperUser(EFS.RC.CC.Presences.Operators.Anonymous annonimousPresence, Microsoft.Rtc.Collaboration.InstantMessagingCall superUserInstantMessagingCall)
			: base(superUserInstantMessagingCall)
		{
			// check
			EFS.Common.Verify.Reference(annonimousPresence, "annonimousPresence");

			// set internals
			this.annonimousPresence = annonimousPresence;

			// log
			EFS.EventLog.Log.LogInfo("Supervisor", this);
		}

		#endregion

		#region Usage

		/// <summary>
		/// Describe this object into a nice formated string
		/// </summary>
		/// <returns></returns>
		public override System.String Describe()
		{
			return System.String.Format("EFS.RC.CC.Applications.Remote.Operators [{0}]", base.Describe());
		}

		#endregion

		#region Startup

		/// <summary>
		/// OnEstablished
		/// </summary>
		protected override void OnEstablished()
		{
			// send monitored customer end points
			BeginDataSend("Helo, I am the contact center, how can I help you?");
		}

		#endregion

		#region Runtime

		/// <summary>
		/// OnMessageReceived
		/// </summary>
		/// <param name="message"></param>
		protected override void OnMessageReceived(EFS.RC.CC.Messages.Message message)
		{
			// check ACD message
			if (message is EFS.RC.CC.Messages.Application.Command)
			{
				// update -> create
				EFS.RC.CC.Tools.Control.Command command = new EFS.RC.CC.Tools.Control.Command(
					(message as EFS.RC.CC.Messages.Application.Command).Text,
					this);

				// execute database update
				command.Execute();
			}
			else
			{
				BeginDataSend("Uknown message");
			}
		}

		/// <summary>
		/// Message received
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected override void OnMessageReceived(System.Object sender, Microsoft.Rtc.Collaboration.InstantMessageReceivedEventArgs e)
		{
			try
			{
				// parse message
				EFS.RC.CC.Tools.Control.Command controlCommand = new EFS.RC.CC.Tools.Control.Command(e.TextBody, this);
				controlCommand.Execute();
			}
			catch (System.Exception ex)
			{
				// just log
				EFS.EventLog.Log.LogException(ex, this);
			}
		}

		#endregion // Runtime

		#region Shutdown

		#endregion
	}
}

